package com.cn.longdistancebusstation.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.cn.longdistancebusstation.R;
import com.kaopiz.kprogresshud.KProgressHUD;

/**
 * Created by Administrator on 2018/1/15.
 */

public class BaseFragment extends Fragment implements View.OnClickListener,AbsListView.OnScrollListener,AdapterView.OnItemClickListener{
    private static final String TAG ="BaseFragment" ;
    private KProgressHUD mKProgressHUD;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.custom_actionbar, container,false);
        // 使用了这个方法以后关于menu的回调函数才会被调用
        setHasOptionsMenu(true);
        return rootView;

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // 设置MenuItem.SHOW_AS_ACTION_IF_ROOM使得在不溢出的情况让选项添加到actionbar上
        menu.add(0, 0, 0, "item1")
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(0, 1, 1, "item2")
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    private void createHud(){
        Log.i("BaseActivity", "createHud");
        mKProgressHUD = KProgressHUD.create(getActivity())
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
    }

    public void showHud(){
        if(mKProgressHUD == null){
            createHud();
        }
        if(!mKProgressHUD.isShowing()){
            mKProgressHUD.show();
        }
    }

    public void showHud(String hint){
        if(mKProgressHUD == null){
            createHud();
        }
        if(hint.length() > 0){
            mKProgressHUD.setLabel(hint);
        }
        if(!mKProgressHUD.isShowing()){
            mKProgressHUD.show();
        }
    }

    public void hideHud(){
        if(mKProgressHUD != null){
            mKProgressHUD.dismiss();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}
